package exception;

import java.io.*;
import java.util.Scanner;

public class LastLife {
    private static BufferedReader br = null;//字段
    private static FileWriter fw = null;

    //读入上次的心情
    public static void init() {
        try {
            br = new BufferedReader(new FileReader("e:\\a.txt"));//打开文件时发生IO异常
            String ki = br.readLine();
            System.out.println("上次心情" + ki);
        } catch (IOException e) {
            System.out.println("这是第一次运行本程序");
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("文件关闭失败!!!");
                }
            }
        }
    }//方法

    //读入此次心情
    public static void term(String ki) {
        try {
            fw = new FileWriter("e:\\a.txt");
            fw.write(ki);//对文件实际执行输入输出时
        } catch (IOException e) {
            System.out.println("发生错误！！");
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    System.out.println("文件关闭失败！！！");
                }


            }
        }


    }


    public static void main(String[] args) {
        Scanner stdIn=new Scanner(System.in);
        init();
        System.out.println("当前心情:");
        String ki=stdIn.nextLine();
        term(ki);
    }

}

